const mongoose = require('mongoose')

const Schema = mongoose.Schema

const ShopSchema = new Schema(
  {
    province: {
      type: String,
      require: true,
    },
    city: {
      type: String,
      require: true,
    },
    address: {
      type: String,
      require: true,
    },
    shopName: {
      type: String,
      require: true,
    },
    tel: {
      type: Number,
      require: true,
    },
  },
  {
    timestamps: { currentTime: () => Date.now() },
  }
)

ShopSchema.virtual('carList', {
  ref: 'Car',
  localField: '_id',
  foreignField: 'shop_id',
  justOne: false, //取Array值- 会把文章对应的评论全部提取出来
})
ShopSchema.set('toObject', { virtuals: true })
ShopSchema.set('toJSON', { virtuals: true })

const ShopModel = mongoose.model('Shop', ShopSchema)

module.exports = ShopModel
